<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/static/include/top2.jsp"%>
<script>document.title="指标数据维护";</script>

<script src="${baseURL}/static/js/jquery.easyui.min.js"></script>
<script src="${baseURL}/static/js/ajaxfileupload.js"></script>
<link href="${baseURL}/static/js/themes/default/easyui.css" rel="stylesheet" type="text/css" />

<c:set var="userAreaId" value="${sessionScope.user.userAreaId}"></c:set>
<c:set var="pid" value="${requestScope.areasMap[sessionScope.user.userAreaId].parentId}"></c:set>

<style type="text/css">
<!--
table {
	margin:0;
	width:0;
	font-family:"微软雅黑";
	font-size:12px;
	color: #FFF;
	border-collapse: collapse;
}
//-->
</style>

<div id="context">
<table id="search_t" style="width:98%">
		<tbody>
			<tr>
				<th><img src="${ctx}/static/images/pages/cen/rig/ico_01.png" align="middle">&nbsp;指标数据维护
				</th>
			</tr>
			<tr class="tab_grey">
				<td>
					<input type="hidden" id="areaId" name="areaId" value="${userAreaId}">
					选择条件：
					
					<input id="country" name="country" type="checkbox" value="0">&nbsp;全国
				<c:choose>
					<c:when test="${pid eq '0'}">
						<span class="tab_text">省(自治区、直辖市):</span>
					</c:when>
					<c:when test="${requestScope.areasMap[pid].parentId eq '0'}">
						<span class="tab_text">地(区、市、州、盟):</span>
					</c:when>
					<c:otherwise>
						<span class="tab_text">县(区、市、旗):</span>
					</c:otherwise>
				</c:choose>
				
				<input type="text" class="tab_textbut" value="${sessionScope.user.userAreaName}" readonly>

				<c:if test="${pid eq '0'}">
					<span class="tab_text">地(区、市、州、盟):</span>
					<select class="tab_textbut" id="areaId2" onchange="queryAreasByParent(this)">
						<option value="">请选择..</option>
						<option value="${requestScope.areasMap['320600000000000'].id}">${requestScope.areasMap['320600000000000'].areaName}</option>
						<option value="${requestScope.areasMap['320700000000000'].id}">${requestScope.areasMap['320700000000000'].areaName}</option>
						<option value="${requestScope.areasMap['320900000000000'].id}">${requestScope.areasMap['320900000000000'].areaName}</option>
					</select>
				</c:if>

				<c:if test="${pid eq '0' || requestScope.areasMap[pid].parentId eq '0'}">
					<span class="tab_text">县(区、市、旗):</span>
					<select class="tab_textbut_100" id="areaId3">
						<option value="">请选择..</option>
						
						<c:if test="${requestScope.areasMap[pid].parentId eq '0'}">
							<c:forEach items="${requestScope.areasMap}" var="area">
								<c:if test="${requestScope.areasMap[pid].id eq area.value.parentId}">
								<option value="${area.value.id}">${area.value.areaName}</option>								
								</c:if>
							</c:forEach>
						</c:if>
						
					</select>
				</c:if>
				<br/>
				<span class="tab_text">指标分类：</span>
				<select id="indicatorType" name="indicatorType" class="tab_textbut">
					<option value="">请选择..</option>
				<c:forEach items="${indicatorType}" var="item">
					<option value="${item.code}">${item.codeName}</option>
				</c:forEach>
				</select>
				
				<span class="tab_text">产业分类：</span>
				<select id="industryType" name="industryType" class="tab_textbut">
					<option value="">请选择..</option>
				<c:forEach items="${industryList.operationResult}" var="item">
					<option value="${item.dl}">${item.dustryName}</option>
				</c:forEach>
				</select>
				
				<span class="tab_text">核算频率：</span>
				<select id="timeType" name="timeType" class="tab_textbut">
					<option value="">请选择..</option>
					<option value="1">年报</option>
					<option value="3">半年报</option>
					<option value="2">季报</option>
				</select>
				<select id="quarter" name="endYear" class="tab_textbut" style="display: none">
					<option value="1">第一季度</option>
					<option value="2">第二季度</option>
					<option value="3">第三季度</option>
					<option value="4">第四季度</option>
				</select>
				<span class="tab_text">年份:</span>
				<select id="year" name="year" class="tab_textbut">
					<option value="">请选择..</option>
				</select>        

				<br/>
				<div style=" position:relative;">
				<span class="tab_text">指标名称：</span>
				<input type="text" class="tab_textbut" id="indicatorName" name="indicatorName">

				<a class="tab_but" href="#" onclick="queryData();return false;">查询</a>
				<a class="tab_but" href="#" onclick="delIndicator();return false;">删除选中</a>
				<a class="tab_but" href="#" onclick="downIndicator();return false;">下载模板</a>
				<a class="tab_but" href="#" >指标导入</a>${msg}
				<input type="file" class="tab_but" style="position:absolute; cursor:pointer; filter:alpha(opacity:0);opacity: 0; top:0; left:550px; height:24px; width:80px" name="fileField" class="file" id="fileField" size="28" onchange="uploadFile(this);return false;" />
				<a class="tab_but" href="#" onclick="addIndicator();return false;">新增</a>
				
				</div>
				</td>
			</tr>
		</tbody>
</table>

<br>
<div id="dataList" style="overflow:auto;width:98%">
<table id="dg" class="easyui-datagrid" style="width:900px;height:400px"   
        data-options="fitColumns:true,singleSelect:true">   
    <thead>   
        <tr>   
            <th data-options="field:'id',width:20,align:'center',checkbox:true">全选</th>        	
            <th data-options="field:'year',width:60,align:'center'">指标年份</th>   
            <th data-options="field:'indicatorName',width:200">指标名称</th>
            <th data-options="field:'sectionName',width:80">指标分类</th>
            <th data-options="field:'indicatorValue',width:80,align:'right'">指标值</th>
            <th data-options="field:'indicatorUnit',width:80">单位</th>
            <th data-options="field:'operate',width:120,align:'center'">操作</th>
        </tr>   
    </thead>   
</table>
</div>

<div id="modDiv" style="display:none">
	<div style="width:400px;height:250px">   
		<table id="listTable" style="width:98%;">
			<thead>
				<tr style="display:none"><td></td></tr>
			</thead>
			<tbody>
				<tr>
					<td align="right" width="150">指标名称：</td>
					<td><span id="indicatorName"></span></td>
				</tr>
				<tr>
					<td align="right">指标核算值：</td>
					<td><input type="text" class="tab_textbut" id="indicatorValue" name="indicatorValue" value=""></td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2" style="text-align:center">
						<a class="tab_but" href="#" onclick="saveEditValue();return false;">保存</a>
						<a class="tab_but" href="#" onclick="winClose();return false;">关闭</a>
					</td>
				</tr>
			</tfoot>
		</table>
	</div>
</div>

<div id="downDiv" style="display:none">
</div>
</div>
<script language="javaScript">

	var pageNum = 1;
	var pageSize = 10;

	$().ready(function(){
		
		$('#win').window('close');

		var startYear = ${requestScope.versionYearList.startYear};
		var endYear = ${requestScope.versionYearList.endYear};
		
		for (var i=startYear; i<=endYear; i++){
			$("#year").append("<option>" + i + "</option>");
		}
		
		$("#timeType").bind("change", function(){
			if (this.value=="2"){	//季报
				$("#quarter").show();
			}else{
				$("#quarter").hide();
			}
		});
		
		$("#indicatorType").change(function(){
			if (this.value!=1 && this.value !=2){
				$("#industryType").attr("disabled", false);
			}else{
				$("#industryType").attr("disabled", true);
			}
		});
	});
	
	function validate(){
		
		$("#areaId").val("${userAreaId}");
		
		if ($("#areaId2").val()!="" && $("#areaId2").val() != undefined){
			$("#areaId").val($("#areaId2").val());
		}
		
		if ($("#areaId3").val()!="" && $("#areaId3").val() != undefined){
			$("#areaId").val($("#areaId3").val());
		}
		if ($("input:checkbox:checked").val()){
			$("#areaId").val("0");	
		}
		return true;
	}
		
	//表单提交查询
	function queryData(){

		validate();
		
		var timeYear;
		var quarter;

		if ($("#timeType").val()=="1"){
			timeYear = 2;
		}else if ($("#timeType").val()=="3"){
			timeYear = 1;
		}else if ($("#timeType").val()=="2"){
			quarter = $("#quarter").val();			
		}
		var quarter;
		
		var url = "${baseURL}/audit/queryIndicatorData";
		var params = {
				pageNum:pageNum, 
				timeYear:timeYear, 
				timeType:$("#timeType").val(), 
				quarter:quarter, 
				areaId:$("#areaId").val(), 
				year:$("#year").val(), 
				section:$("#indicatorType").val(),
				industryCode:$("#industryType").val(),
				indicatorName:$("input[id='indicatorName']").val()
		};
		
		$('#dg').datagrid({
		    url:url,
		    selectOnCheck:false,
		    checkOnSelect:false,
		    pagination:true,
		    pageSize:pageSize,
		    rownumbers:true,
		    queryParams:params,
		    columns:[[
				{field:'id',width:60,align:'center',title:'全选',checkbox:true},
		        {field:'year',width:60,align:'center',title:'指标年份'},
		        {field:'indicatorName',width:200, title:'指标名称'}, 
		        {field:'sectionName',width:80, title:'指标分类'},   
		        {field:'indicatorValue',width:80,align:'right',title:'指标值'}, 
		        {field:'indicatorUnit',width:80,align:'center',title:'单位'}, 
		        {field:'operate',width:100,align:'center',title:'操作'}
		    ]],
		    loadFilter: function(data){
		    	data.rows = data.operationResult;
		    	data.total = data.totalCount;
		    	
		    	$.each(data.operationResult, function(i, row){
		    		
		    		if (row.isSummaryIndicator!=1){		//总值不可修改操作
			    		row.operate = "<a href='#' onclick='modEditValue(" + i + ");'>修改</a>";
		    		}
		    	});
		    	
		    	
		    	return data;
			}
		}); 
	}

	
	var row_obj;
	
	var modHtml;
	
	var rowIndex;
	
	//加载修正值修改窗口
	function modEditValue(index){
		
		rowIndex = index;
		$("#dg").datagrid("selectRow", index);	//先选中需要修改的行，再获取行信息
		row_obj = $("#dg").datagrid("getSelected", index);	//根据行索引获取行信息
				
		modHtml = $("#modDiv").html();
		
		$.layer({
			type:1,
			title:'修改' + row_obj.indicatorName,
			area: ['400px', '250px'],
			page:{html:modHtml},
			end: function(){
				$("#modDiv").html(modHtml);
			}
		});
		
		$("#modDiv").empty();

		
		$("span[id='indicatorName']").text(row_obj.indicatorName);
		$("#indicatorValue").val(row_obj.indicatorValue);

	}
	
	//保存指标值方法
	function saveEditValue(){

		var indicatorValue = $("#indicatorValue").val();
		
		var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
		
		if (!exp.test(indicatorValue)){
			layer.alert("请输入正确格式的数字，并且只能有两位小数");
			return;
		}

		var quarter;

		if (row_obj.timeType=="2"){	//如果为季报，获取季度信息
			quarter = $("#quarter").val();			
		}
		
		var data = {
			id:row_obj.id,
			areaId:row_obj.areaId,
			timeType:row_obj.timeType,
			year:row_obj.year,
			month:row_obj.month,
			yearTime:row_obj.yearTime,
			quarter:row_obj.quarter,
			indicatorValue:indicatorValue,
			createPreId:'${sessionScope.user.id}'
		};
		
		var url = "${baseURL}/audit/updateIndicator";

		$.post(url, data, function(msg){
			layer.msg("保存成功。", 1, 1, function(){
				
				$("#dg").datagrid("updateRow", {index:rowIndex, row:{indicatorValue:indicatorValue}});
				
				winClose();
			});
		}, 'json');

	}
	
	function winClose(){
		layer.closeAll();	
	}
	
	
	function queryAreasByParent(obj){
		$.getJSON("${baseURL}/area/queryAreasByParent/" + obj.value, function(json){
			$("#areaId3").empty();
			$("#areaId3").append("<option value=''>请选择..</option>");
			$.each(json, function(i, area){
				$("#areaId3").append("<option value='" + area.id + "'>" + area.areaName + "</option>");
			});
		});
	}
	
	//删除选择的指标
	function delIndicator(){
		var ids = new Array();
		var rows = $("#dg").datagrid("getChecked");
		
		if (rows.length==0){
			layer.alert("请勾选需要删除的指标");
			return ;
		}
		
		layer.confirm("请确定是否要勾选的指标？", function(){
			$.each(rows, function(i, row){
				ids.push(row.id);
			});
			
			var url = "${baseURL}/audit/removeIndicator";
			$.post(url, {ids:ids.join(",")}, function(msg){
				if (msg=="success"){
					layer.msg("删除成功", 1, 1, function(){

						$.each(rows, function(i){
							$("#dg").datagrid("deleteRow", i);
						});
						
					});
				}else{
					layer.msg("删除失败", 1);
				}
			}, 'json');
		});
	}
	
	//添加指标
	function addIndicator(){
		var url = "${baseURL}/audit/indicatorAddToPage";
		openWin("指标录入", url, 650, 300);		
	}

	//指标模板下载
	function downIndicator(){
		
		var indicatorType = $("#indicatorType").val();
		var industryType = $("#industryType").val();
		
		if (indicatorType == ""){
			layer.alert("请选择指标类型");
			return ;
		}
		
		if (indicatorType==1 || indicatorType==2){
			industryType = "";
		}
		
		var url = "${baseURL}/indicator-stat.do?method=indicatorTemple&provinceId=" + indicatorType + "&cityId=" + industryType;
		
		var iframe = document.createElement("iframe");
		iframe.src = url;
		document.getElementById("downDiv").appendChild(iframe);
	}
	
	//指标导入
	function uploadFile(obj){

		var url = "${baseURL}/audit/importIndicator/${sessionScope.user.id}";

		$.ajaxFileUpload({
			url:url,
			fileElementId:'fileField',
			secureuri:false,
			dataType:'json',
			type:'post',
			success: function(msg){
				layer.msg(msg, 1, 1);
			},
			error: function(msg){
				
			}
		});
		
	}
	
</script>




<%@ include file="/static/include/bottom.jsp"%>